package com.limingfeiyu.platform.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.limingfeiyu.common.web.PageTable;
import com.limingfeiyu.common.web.ResultBody;
import org.springframework.web.bind.annotation.RestController;
import com.limingfeiyu.platform.management.service.ISysMenuService;
import com.limingfeiyu.platform.management.entity.SysMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;

/**
 * <p>
 * 系统菜单 前端控制器 vm
 * </p>
 *
 * @author 黎铭飞羽
 * @since 2024-03-02
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

	@Autowired
    private ISysMenuService service;

    // 分页列表
    @PostMapping("/getPageList")
    @ApiOperation(value = "获取分页列表", notes = "pageList")
    @ApiImplicitParam(name = "pageTable", value = "分页表格",
            required = true, type = "ResultBody")
    public ResultBody<Page<SysMenu>> getPageList(@RequestBody PageTable<SysMenu> pageTable) throws Exception {
        if (pageTable == null) {
            pageTable = new PageTable<>();
            pageTable.setParam(new SysMenu());
        }
        return ResultBody.ok().data(service.pageList(pageTable));
    }

    // 更新保存
    @PostMapping("/USave")
    @ApiOperation(value = "更新", notes = "ResultBody")
    @ApiImplicitParam(name = "obj", value = "",
            required = true, type = "ResultBody")
    public ResultBody<SysMenu> USave(@RequestBody SysMenu obj) {
        if (service.updateById(obj)) {
            return ResultBody.ok().data(obj);
        } else {
            return ResultBody.alert().msg("更新失败！").data(obj);
        }
    }

    // 删除
    @PostMapping("/DSave/{id}")
    @ApiOperation(value = "删除", notes = "ResultBody")
    @ApiImplicitParam(name = "id", value = "",
            required = true, type = "ResultBody")
    public ResultBody DSave(@PathVariable("id") Long id) {
        if (service.removeById(id)) {
            return ResultBody.ok();
        } else {
            return ResultBody.alert().msg("删除失败！").data(new HashMap<String, Object>().put("id", id));
        }
    }

    // 新增
    @PostMapping("/ASave")
    @ApiOperation(value = "新增", notes = "ResultBody")
    @ApiImplicitParam(name = "obj", value = "",
            required = true, type = "ResultBody")
    public ResultBody<SysMenu> ASave(@RequestBody SysMenu obj) throws Exception {
        if (service.save(obj)) {
            return ResultBody.ok().data(obj);
        } else {
            return ResultBody.alert().msg("新增失败！").data(obj);
        }

    }

}

